<template>
	<view class="tabs-container">
		<tabsTop tabName="订阅消息"></tabsTop>
		<view class="tabs">
			<view class="tab" :class="{ active: currentTab === 'daily' }" @click="setCurrentTab('daily')">销售日报
			</view>
			<view class="tab" :class="{ active: currentTab === 'analyse' }" @click="setCurrentTab('analyse')">
				销售趋势分析</view>
			<view class="tab" :class="{ active: currentTab === 'fund' }" @click="setCurrentTab('fund')">
				掌上资金</view>
		</view>

		<view v-if="currentTab === 'daily'" class="content">
			<view class="charts-box">
				<view style="margin: 5px;margin-top: 12px;">销售日报</view>
				<view class="table">
					<view class="tr">
						<view class="th"></view>
						<view class="th">订单数</view>
						<view class="th">销售额/元</view>
						<view class="th">利润/元</view>
					</view>

					<view class="tr" v-for="(item,index) in table" :key="index">
						<view class="td">{{item.Sales_Date}}</view>
						<view class="td">{{item.Quantity}}单</view>
						<view class="td">{{formatNumber(item.Sales_Amount)}}元</view>
						<view class="td">{{item.Profit}}%</view>
					</view>

				</view>
				<view style="margin: 5px;margin-top: 12px;">待处理订单销售量</view>
				<view class="table">
					<view class="tr">
						<view class="th"></view>
						<view class="th">待处理订单数</view>
						<view class="th">销售额/元</view>
					</view>

					<view class="tr" v-for="(item,index) in tableData" :key="index">
						<view class="td">{{item.Sales_Date}}</view>
						<view class="td">{{item.Quantity}}单</view>
						<view class="td">{{formatNumber(item.Sales_Amount)}}元</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="currentTab === 'analyse'" class="content">
			<view class="charts-box">
				<view style="margin-top: 10px;">
					<view>
						<qiun-data-charts type="mix" :opts="opts" :chartData="chartData" />
					</view>
				</view>
				<view class="table">
					<view class="tr">
						<view class="th">月份</view>
						<view class="th">销售收入</view>
						<view class="th">利润</view>
						<view class="th">利润率</view>
					</view>
					<view class="tr" v-for="(item, index) in chartData.categories" :key="index">
						<view class="td">{{item}}</view>
						<view class="td">{{formatNumber(getChartDataByName('销售输入')[index])}}元</view>
						<view class="td">{{formatNumber(getChartDataByName('利润')[index])}}元</view>
						<view class="td">{{getChartDataByName('利润率')[index]}}%</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="currentTab === 'fund'" class="content">
			<view class="charts-box">
				<view class="amount">
					<text>￥{{formatNumber(amount)}}</text>
				</view>
				<view class="bill" v-for="(item, index) in calendarData" :key="index">
					<view class="calendar">
						<!-- 日历部分 -->
						<view class="calendar-header font-md" style="background-color: #ea6d4f;">
							{{ getFormattedDate(item.dateRange) }}
						</view>
						<view class="calendar-content font-lg">
							{{ item.text }}
						</view>
					</view>
					<view class="data">
						<view>
							<view>收入</view>
							<view>支出</view>
							<view>结余</view>
						</view>
						<view style="text-align: end;">
							<view>￥{{ formatNumber(item.income) }}</view>
							<view>￥{{ formatNumber(item.expense) }}</view>
							<view style="color: #f7bd8b;">￥{{ formatNumber(item.balance) }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {WeChatProfitAnalysis} from '@/api/report_forms.js';
	import tabsTop from "@/components/tabs/tabs-top.vue";
	export default {
		components: {
			tabsTop
		},
		data() {
			const today = new Date();
			const startOfWeek = new Date(today);
			// const day = today.getDay() || 7;
			const firstDayOfWeek = new Date(startOfWeek.setDate(today.getDate() - today.getDay() + 1));
			const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
			const lastDayOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
			const firstDayOfYear = new Date(today.getFullYear(), 0, 1);
			return {
				currentTab: 'daily',
				table: [{
						Sales_Date: '本日',
						Quantity: '6',
						Sales_Amount: '1340',
						Profit: '67'
					},
					{
						Sales_Date: '本周',
						Quantity: '6',
						Sales_Amount: '1315',
						Profit: '67'
					},
					{
						Sales_Date: '本月',
						Quantity: '6',
						Sales_Amount: '1315',
						Profit: '67'
					}
				],
				tableData: [{
						Sales_Date: '本日',
						Quantity: '1',
						Sales_Amount: '50',
					},
					{
						Sales_Date: '本周',
						Quantity: '1',
						Sales_Amount: '50',
					},
					{
						Sales_Date: '本月',
						Quantity: '1',
						Sales_Amount: '50',
					}
				],
				chartData: {},
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4","#ea7ccc"],
					title: '图表（合并）',
					padding: [10, 20, 0, 0],
					enableScroll: false,
					legend: {},
					xAxis: {
						disableGrid: true,
						title: "单位:月"
					},
					yAxis: {
						disabled: false,
						disableGrid: false,
						splitNumber: 5,
						gridType: "dash",
						dashLength: 4,
						gridColor: "#CCCCCC",
						padding: 10,
						showTitle: true,
						data: [{
							position: "right",
							title:'利润率',
							max: 100,
							min: 0,
						},{
							position: "left",
							min: 0,
							title: "销售额",
							textAlign: "left"
						}]
					},
					extra: {
						mix: {
							column: {
								width: 20
							}
						}
					}
				},
				amount: '258',
				calendarData: [{
						dateRange: today,
						text: '今天',
						income: '258',
						expense: '222',
						balance: '36'
					},
					{
						dateRange: [firstDayOfWeek, today],
						text: '本周',
						income: '258',
						expense: '222',
						balance: '36'
					},
					{
						dateRange: [firstDayOfMonth, today],
						text: '本月',
						income: '258',
						expense: '222',
						balance: '36'
					},
					{
						dateRange: [firstDayOfYear, today],
						text: '今年',
						income: '258',
						expense: '222',
						balance: '36'
					}
				]
			};
		},
		onReady() {
			this.getServerData();
		},
		methods: {
			setCurrentTab(tab) {
				this.currentTab = tab;
			},
			async getServerData() {
				//模拟从服务器获取数据时的延时
				const res = await WeChatProfitAnalysis({Enterprise_Info_SerialNumber:'E00000000000019'});
				this.chartData = res
				// console.log(res);
				// setTimeout(() => {
				// 	//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
				// 	let res = {
				// 		categories: ["1月", "2月", "3月"],
				// 		series: [{
				// 				name: "销售输入",
				// 				index: 1,
				// 				type: "column",
				// 				data: [1110, 1240, 588]
				// 			},
				// 			{
				// 				name: "利润",
				// 				index: 1,
				// 				type: "column",
				// 				data: [1650, 1314, 838]
				// 			},
				// 			{
				// 				name: "利润率",
				// 				type: "line",
				// 				color: "#2fc25b",
				// 				data: [20, 10, 40]
				// 			},
				// 		]
				// 	};
					
				// }, 500);
			},
			getChartDataByName(name) {
				if (this.chartData && this.chartData.series) {
					const series = this.chartData.series.find((item) => item.name === name);
					if (series) {
						return series.data;
					}
				}
				return [];
			},
			getFormattedDate(dateRange) {
				if (Array.isArray(dateRange)) {
					const startDate = dateRange[0];
					const endDate = dateRange[1];
					const formattedStartDate = `${startDate.getMonth() + 1}.${startDate.getDate()}`;
					const formattedEndDate = `${endDate.getMonth() + 1}.${endDate.getDate()}`;
					return `${formattedStartDate}-${formattedEndDate}`;
				} else {
					const formattedDate = `${dateRange.getMonth() + 1}.${dateRange.getDate()}`;
					return formattedDate;
				}
			},
			formatNumber(number) {
				// 将数字转为千位分隔符格式，保留两位小数
				return Number(number).toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			},
		}
	};
</script>

<style>
	.tabs-container {
		margin: 10rpx;
	}

	.tabs {
		border: 2px solid #3399FF;
		display: flex;
		justify-content: space-between;
		border-radius: 7px;
	}

	.tab {
		flex: 1;
		text-align: center;
		padding: 7rpx;
		background-color: #f0f0f0;
		color: #333;
		cursor: pointer;
		height: 23px;
	}

	.active {
		background-color: #3399FF;
		color: #fff;
	}

	.content {
		/* margin-top: 20rpx; */
		text-align: center;
	}

	.charts-box {
		width: 100%;
		height: 300px;
		/* 		margin-top: 20px; */
	}

	.table {
		width: 100%;
		border-radius: 8rpx;
		display: table;
		border: 1px solid #f7f7f7;
		border-collapse: collapse;
	}

	.th {
		text-align: center;
		color: #FFFFFF;
		padding: 20rpx 0;
		font-weight: bolder;
		display: table-cell;
		border: 1px solid #f7f7f7;
		background-color: #f86f10;
		font-size: 15px;
	}

	.td {
		text-align: center;
		background: #FFFFFF;
		padding: 20rpx 0;
		display: table-cell;
		border: 1px solid #dedede;
	}

	.tr {
		display: table-row;
	}

	.amount {
		height: 50px;
		width: 100%;
		background-color: #ea6d4f;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.amount text {
		color: white;
		font-size: 20px;
		text-align: center;
	}

	.bill {
		display: flex;
		justify-content: space-between;
		width: 90%;
		margin: auto;
		height: 100px;
		margin-top: 20px;
		align-items: center;
	}

	.calendar {
		margin-bottom: 10px;
		width: 32%;
		position: relative;
	}

	.calendar-header {
		color: #fff;
		text-align: center;
		height: 28px;
		border-top-left-radius: 5px;
		border-top-right-radius: 5px;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
	}

	.calendar-header::before,
	.calendar-header::after {
		content: '';
		position: absolute;
		top: 95%;
		transform: translateY(-50%);
		width: 5px;
		height: 10px;
		background-color: white;
		border-radius: 40%;
		border: 1px solid #dedede;
	}

	.calendar-header::before {
		left: 75px;
	}

	.calendar-header::after {
		right: 75px;
	}

	.calendar-content {
		padding: 10px;
		text-align: center;
		border-left: 1px solid #c3c3c3;
		border-bottom: 1px solid #c3c3c3;
		border-right: 1px solid #c3c3c3;
		height: 35px;
		border-bottom-left-radius: 5px;
		border-bottom-right-radius: 5px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.data {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 64%;
		line-height: 28px;
	}
</style>